package com.jc.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.jc.subject.common.enums.CategoryTypeFlagEnum;
import com.jc.subject.common.enums.IsDeletedFlagEnum;
import com.jc.subject.domain.convert.SubjectCategoryConverter;
import com.jc.subject.domain.convert.SubjectLabelConverter;
import com.jc.subject.domain.entity.SubjectCategoryDO;
import com.jc.subject.domain.entity.SubjectLabelDO;
import com.jc.subject.domain.service.SubjectCategoryDomainService;
import com.jc.subject.domain.service.SubjectLabelDomainService;
import com.jc.subject.infra.basic.entity.SubjectCategory;
import com.jc.subject.infra.basic.entity.SubjectLabel;
import com.jc.subject.infra.basic.entity.SubjectMapping;
import com.jc.subject.infra.basic.service.SubjectCategoryService;
import com.jc.subject.infra.basic.service.SubjectLabelService;
import com.jc.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author gongjiawei
 * @Date 2024/6/5 21:22
 * @注释
 */


@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

@Resource
private SubjectLabelService subjectLabelService;

@Resource
private SubjectMappingService subjectMappingService;

@Resource
private SubjectCategoryService subjectCategoryService;
    @Override
    public void add(SubjectLabelDO subjectLabelDO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelDO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectLabelService.insert(subjectLabel);
    }

    @Override
    public List<SubjectLabelDO> queryLabel(SubjectLabelDO subjectLabelDO) {

        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelDO.getCategoryId());

        if (subjectCategory.getCategoryType()== CategoryTypeFlagEnum.PRIMARY.getCode()){
            SubjectLabel subjectLabel=new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelDO.getCategoryId());
            subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());


            List<SubjectLabel>subjectLabelList= subjectLabelService.queryByCondition(subjectLabel);

            return SubjectLabelConverter.INSTANCE.convertLabelListToDoList(subjectLabelList);
        }

        SubjectMapping subjectMapping=new SubjectMapping();
        subjectMapping.setCategoryId(subjectLabelDO.getCategoryId());
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        List<SubjectMapping>subjectMappingList=subjectMappingService.queryMappings(subjectMapping);

        if (subjectMappingList.isEmpty()){
            return Collections.emptyList();
        }
        List<Long> collect = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        List<SubjectLabel>subjectLabels=subjectLabelService.batchquery(collect);
        List<SubjectLabelDO> subjectLabelDOS = SubjectLabelConverter.INSTANCE.convertLabelListToDoList(subjectLabels);

        subjectLabelDOS.forEach(label->{label.setCategoryId(subjectLabelDO.getCategoryId());});
        return subjectLabelDOS;
    }

    @Override
    public Boolean update(SubjectLabelDO subjectLabelDO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelDO);

        int update = subjectLabelService.update(subjectLabel);
        return update>0;

    }

    @Override
    public Boolean delete(SubjectLabelDO subjectLabelDO) {
        if (log.isInfoEnabled()){
            log.info("deleteLabel,message:{}", JSON.toJSONString(subjectLabelDO));
        }

        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelDO);

        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int update = subjectLabelService.update(subjectLabel);
        return update>0;
    }



}
